package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IQa;
import com.wbao.dianniu.listener.IQaListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 问答
 *
 * @author yejianjun
 */
public class QaManager implements IQa {
    private IQaListener mListener = null;
    private Context mContext;

    public QaManager(Context context) {
        mContext = context;
    }

    @Override
    public boolean addQaListener(IQaListener listener) {
        mListener = listener;
        return mListener != null;
    }

    @Override
    public boolean removeQaListener(IQaListener listener) {
        if (null == listener)
            return false;
        mListener = null;
        return true;
    }

    /**
     * @param accountId    用户ID，当前登陆用户的ID
     * @param searchKey    需要搜索的内容
     * @param type         1:电纽问答，2:匿名问答,3:我发起的的问题,31我参与的问题,4:我发起的匿名,41我参与的匿名,5:我的收藏 10:心情
     * @param page         分页页码,默认值为0
     * @param rows         每页多少条
     * @param lastUpdateId 非热门的第一条questId
     * @param refresh      up上拉刷新，down下拉刷新
     */
    @Override
    public void reqData(int accountId, String searchKey, int type, int page, int rows, Integer lastUpdateId, String refresh) {
        StringEntity stringEntity = null;
        try {
            Batch batch = new Batch();
            batch.setToken(GlobalContext.getCacheToken());
            Command command = new Command();
            command.addAction(Action.ACTION_ACCOUNT_QUEST_LIST)
                    .addArgs("accountId", accountId)
                    .addArgs("searchKey", searchKey)
                    .addArgs("type", type)
                    .addArgs("page", page)
                    .addArgs("rows", rows)
                    .addArgs("lastUpdateId", lastUpdateId)
                    .addArgs("refresh", refresh);
            batch.addCommand(command);
            stringEntity = new StringEntity(batch.toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new MyResponse() {

            @Override
            public void responseSucess(Object obj) {
                // TODO Auto-generated method stub
                List<QaResponse> foosList = JsonUtil.toObjectList(obj, QaResponse.class);
                // 通知界面
                if (null != mListener)
                    mListener.onQaSuccess(foosList);
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                // TODO Auto-generated method stub
                if (null != mListener)
                    mListener.onQaFailure(errorCode, msg);
            }
        });
    }

}
